Conditions | 10 |
Paths | 132 |
Total Lines | 75 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Request.performRequest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* jshint -W100, -W071 */ |
||
128 | Request.prototype.performRequest = function(options) { |
||
129 | var self = this; |
||
130 | var method = options.method; |
||
131 | var signHMAC = false; |
||
132 | |||
133 | if (options.auth === 'http-signature') { |
||
134 | signHMAC = true; |
||
135 | delete options.auth; |
||
136 | } |
||
137 | |||
138 | var uri = (self.https ? 'https://' : 'http://') + options.hostname + options.path; |
||
139 | |||
140 | var request = superagent(method, uri); |
||
141 | |||
142 | if (self.payload && (method === 'DELETE' || method === 'POST' || method === 'PUT' || method === 'PATCH')) { |
||
143 | request.send(self.payload); |
||
144 | } |
||
145 | |||
146 | _.forEach(options.headers, function(value, header) { |
||
147 | request.set(header, value); |
||
148 | }); |
||
149 | |||
150 | if (signHMAC) { |
||
151 | if (!self.apiSecret) { |
||
152 | var error = new Error("Missing apiSecret! required to sign POST requests!"); |
||
153 | self.deferred.reject(error); |
||
154 | return self.callback(error); |
||
155 | } |
||
156 | |||
157 | request.use(superagentHttpSignature({ |
||
158 | headers: ['(request-target)', 'content-md5'], |
||
159 | algorithm: 'hmac-sha256', |
||
160 | key: self.apiSecret, |
||
161 | keyId: self.apiKey |
||
162 | })); |
||
163 | } |
||
164 | |||
165 | request.end(function(error, res) { |
||
166 | var body; |
||
167 | |||
168 | if (error) { |
||
169 | var err = Request.handleFailure(error.response && error.response.body, error.status); |
||
170 | |||
171 | self.deferred.reject(err); |
||
172 | return self.callback(err, error.response && error.response.body); |
||
173 | } |
||
174 | |||
175 | debug('response status code: %s content type: %s', res.status, res.headers['content-type']); |
||
176 | if (!error && (res.headers['content-type'].indexOf('application/json') >= 0)) { |
||
177 | try { |
||
178 | body = JSON.parse(res.text); |
||
179 | } catch (e) { |
||
180 | error = e; |
||
181 | } |
||
182 | } |
||
183 | |||
184 | if (!body) { |
||
185 | body = res.text; |
||
186 | } |
||
187 | |||
188 | if (!error && res.status !== 200) { |
||
189 | error = Request.handleFailure(res.text, res.statusCode); |
||
190 | } |
||
191 | |||
192 | if (error) { |
||
193 | self.deferred.reject(error); |
||
194 | } else { |
||
195 | self.deferred.resolve(body); |
||
196 | } |
||
197 | |||
198 | return self.callback(error, body); |
||
199 | }); |
||
200 | |||
201 | return self.deferred; |
||
202 | }; |
||
203 | |||
248 |